set more off
clear all
set type double
cd "C:\Users\rodin\VenAnalytics Dropbox\VenAnalytics\Research\Papers\2025\Migration\"
capture: log close


log using migration.smcl, replace

use data.dta, clear

*** Data setup  ***
label var brent_eia "Brent Price"
label var sbo_enc "Encounters"


gen monthstart = mofd(date)
gen nextmonthstart = monthstart + 1
gen daysinmonth = dofm(nextmonthstart) - dofm(monthstart)

gen rev= oil_production_mbd_secondary* brent_eia*daysinmonth/1000
label var rev "Revenues"
gen lnrev=ln(rev)
gen lnp=ln(brent_eia)
gen lnenc_v=ln(sbo_enc)
gen t = mofd(date)
gen month=month(date)
gen year=year(date)
gen dummyvar = .
gen dummyvar2 = .

gen unemp=UNRATE

tsset t, m
keep if tin(2020m10,2024m9)


*** Figure A1: Replication of BH Figure 1 ***

twoway (line sbo_enc t, lw(thin) lc(gold) ylabel(, angle(vertical))) (line rev t, lw(thin) lc(blue) lp(dash) yaxis(2) ylabel(, angle(vertical) axis(2))),  xti("") name(p1) xlab(,alt) nodraw legend(position(12)) legend(label(1 "Encounters"))  legend(label(2 "Oil revenues"))

twoway (line D12.sbo_enc t, lw(thin) lc(gold) ylabel(, angle(vertical))) (line D12.rev t, lw(thin) lc(blue) lp(dash) yaxis(2) ylabel(, angle(vertical) axis(2))) if t>=tm(2021m10),  xti("") name(p2) xlab(,alt) title("Encounters and oil income, original") nodraw legend(position(12)) legend(label(1 "Encounters, 12-th difference"))  legend(label(2 "Oil revenues, 12-th difference"))

twoway (line sbo_enc t, lw(thin) lc(gold) ylabel(, angle(vertical))) (line brent_eia t, lw(thin) lc(blue) lp(dash) yaxis(2) ylabel(, angle(vertical) axis(2))),  xti("") name(p3) xlab(,alt) nodraw legend(position(12)) legend(label(1 "Encounters"))  legend(label(2 "Oil prices"))

twoway (line D12.sbo_enc t, lw(thin) lc(gold) ylabel(, angle(vertical))) (line D12.brent_eia t, lw(thin) lc(blue) lp(dash) yaxis(2) ylabel(, angle(vertical) axis(2))) if t>=tm(2021m10),  xti("") name(p4) xlab(,alt) title("Encounters and oil price, original") nodraw legend(position(12)) legend(label(1 "Encounters, 12-th difference"))  legend(label(2 "Oil prices, 12-th difference"))

graph combine p1 p2 p3 p4, cols(2) altshrink saving(fig0.gph, replace) title("Figure A1: Replication of Bahar-Hausmann Figure 1")


*** Figure 2: Original and Corrected BH YOY Figures ***

twoway (line D12.sbo_enc t, lw(thin) lc(gold) ylabel(, angle(vertical))) (line D12.rev t, lw(thin) lc(blue) lp(dash) yaxis(2) ylabel(, angle(vertical) axis(2))) if t>=tm(2021m10),  xti("") name(ur) xlab(,alt) title("Encounters and oil income, original") nodraw legend(position(12)) legend(label(1 "Encounters, 12-th difference"))  legend(label(2 "Oil revenues, 12-th difference"))

twoway (line D12.sbo_enc t, lw(thin) lc(gold) ylabel(, angle(vertical))) (line D12.brent_eia t, lw(thin) lc(blue) lp(dash) yaxis(2) ylabel(, angle(vertical) axis(2))) if t>=tm(2021m10),  xti("") name(lr) xlab(,alt) title("Encounters and oil price, original") nodraw legend(position(12)) legend(label(1 "Encounters, 12-th difference"))  legend(label(2 "Oil prices, 12-th difference"))

twoway (line S12.sbo_enc t, lw(thin) lc(gold) ylabel(, angle(vertical))) (line S12.rev t, lw(thin) lc(blue) lp(dash) yaxis(2) ylabel(, angle(vertical) axis(2))) if t>=tm(2021m10),  xti("") name(cur) xlab(,alt) title("Encounters and oil income, corrected") nodraw legend(position(12)) legend(label(1 "Encounters, year-over-year difference"))  legend(label(2 "Oil revenues, year-over-year difference"))

twoway (line S12.sbo_enc t, lw(thin) lc(gold) ylabel(, angle(vertical))) (line S12.brent_eia t, lw(thin) lc(blue) lp(dash) yaxis(2) ylabel(, angle(vertical) axis(2))) if t>=tm(2021m10),  xti("") name(clr) xlab(,alt) title("Encounters and oil price, corrected") nodraw legend(position(12)) legend(label(1 "Encounters, year-over-year difference"))  legend(label(2 "Oil prices, year-over-year difference"))




graph combine ur cur lr clr, cols(2) altshrink saving(fig1.gph, replace) title("Figure 2: Original and Corrected Bahar-Hausmann Figures")



*** Table 3: Original and corrected difference specifications  ***


reg lnenc_v lnrev, robust
predict res, resid
reg D12.lnenc_v D12.lnrev D12L.lnenc_v D12L.lnrev L.res, robust
estimates store est1


reg lnenc_v lnp, robust
predict res1, resid
reg D12.lnenc_v D12.lnp D12L.lnenc_v D12L.lnp L.res1, robust

estimates store est2



reg lnenc_v lnrev, robust
predict res2, resid
reg S12.lnenc_v S12.lnrev S12L.lnenc_v S12L.lnrev L.res2, robust
estimates store est3

reg lnenc_v lnp, robust
predict res3, resid
reg S12.lnenc_v S12.lnp S12L.lnenc_v S12L.lnp L.res3, robust
estimates store est4


estimates table est1 est2 est3 est4, b(%9.2f) se(%9.2f) keep(D12.lnrev D12.lnp S12.lnrev S12.lnp) stats(N r2)
estimates table est1 est2 est3 est4, b(%9.2f) star(.10 .05 .01) keep(D12.lnrev D12.lnp S12.lnrev S12.lnp) stats(N r2)

*** Table 4: Levels specification with and without US unemployment rate controls ***

reg lnenc_v unemp i.month i.year, robust
estimates store est5

reg lnenc_v lnrev i.month i.year, robust
estimates store est6

reg lnenc_v lnrev unemp i.month i.year, robust
est store est7

reg lnenc_v lnp i.month i.year, robust
estimates store est8

reg lnenc_v lnp unemp i.month i.year, robust
est store est9


estimates table est6 est8 est5 est7 est9, b(%9.2f) se(%9.2f) keep(lnrev lnp unemp) 
estimates table est6 est8 est5 est7 est9, b(%9.2f) star(.10 .05 .01) keep(lnrev lnp unemp) stats(N r2)




*** Figure 3: Oil prices and US unemployment

gen str_date = subinstr(string(date, "%tdMon_CCYY"), " ", "-", .)
scatter brent_eia unemp,     mlabel(str_date)   mlabsize(vsmall) mlabcolor(blue) ytitle("Brent price") xtitle("US unemployment rate")   || lfit brent_eia unemp, lw(medthick) lc(black)  xlabel(#10, format(%9.0g))  legend(off)  title("Figure 3: Oil prices and US unemployment rate,2020-2024") saving(figure2.gph, replace)



log close
